package com.bzkj.web.controller.wms;

import com.bzkj.basicinfo.domain.wms.WmsStoStockEntity;
import com.bzkj.basicinfo.domain.wms.dto.FreezeDto;
import com.bzkj.basicinfo.domain.wms.dto.WmsStoStockDto;
import com.bzkj.basicinfo.domain.wms.vo.RequestStoStockVo;
import com.bzkj.basicinfo.service.wms.WmsStoStockService;
import com.bzkj.basicinfo.service.stock.WmsStockService;
import com.bzkj.common.annotation.LogC;
import com.bzkj.common.core.domain.AjaxResult;
import com.bzkj.common.core.domain.R;
import com.bzkj.common.enums.BusinessType;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Date 2025/01/02 09:47:24
 * @description 库存表
 */
@RestController
@RequestMapping("wms/stoStock")
public class WmsStoStockController {
    @Autowired
    private WmsStoStockService wmsStoStockService;

    @Autowired
    private WmsStockService wmsStockService;

    /**
     * 设置出库状态
     *
     * @param
     * @param
     * @return
     */
    @PreAuthorize("@ss.hasPermi('organize:verifyOrg:edit') || @ss.hasPermi('organize:goodsOrg:frozen')")
    @PostMapping("setOutboundLimit")
    public R setOutboundLimit(@RequestBody WmsStoStockDto entity) {
        return wmsStoStockService.setOutboundLimit(entity);
    }

    /**
     * 更新出库信息
     *
     * @param entity
     * @return
     */
    @ApiOperation("效期修改接口")
    @PreAuthorize("@ss.hasPermi('organize:goodsOrg:edit') || @ss.hasPermi('storeM:storeShift:edit')")
    @PutMapping("updateOutboundLimit")
    @LogC(title = "商品管理-效期修改", businessType = BusinessType.UPDATE, tableNames = "wms_sto_stock",
            pkFieldNames = "pk_material,pk_batchcode,cwarehouse_id,pk_product", idFieldName = "pk_material",
            pkFileds = "#entity.pkMaterial+','+#entity.pkBatchcode+','+#entity.cwarehouseId+','+#entity.cproductorid",
            content = "'商品名称: '+#entity.name+',批号: '+#entity.vbatchcode")
    public AjaxResult updateOutboundLimit(@RequestBody WmsStoStockEntity entity) {
        return wmsStoStockService.updateOutboundLimit(entity);
    }

    /**
     * 冻结操作
     *
     * @param dto
     * @return
     */
    @ApiOperation("冻结接口")
    @PostMapping("freeze")
    @LogC(title = "冻结数据", businessType = BusinessType.UPDATE,
            tableNames = "wms_sto_stock",pkFieldNames = "pk_material,pk_batchcode,cwarehouse_id,pk_product", idFieldName = "pk_material",
            pkFileds = "#dto.pkMaterial+','+#dto.pkBatchcode+','+#dto.cwarehouseId+','+#dto.cproductorid",
            content = "'商品名称: '+#dto.name+',批号: '+#dto.vbatchcode")
    public AjaxResult freeze(@RequestBody FreezeDto dto) {
        return wmsStoStockService.freeze(dto);
    }


    /**
     * 请验管理状态修改
     *
     * @param stoStockVo
     * @return
     */
    @ApiOperation("修改状态接口")
    @PutMapping("updateState")
    @LogC(title = "库存状态", businessType = BusinessType.UPDATE, tableNames = "wms_sto_stock",
            pkFieldNames = "pk_material,vbatchcode,cwarehouse_id,pk_product", idFieldName = "pk_material",
            pkFileds = "#stoStockVo.pkMaterial+','+#stoStockVo.vbatchcode+','+#stoStockVo.cwarehouseid+','+#stoStockVo.cproductorid",
            content = "'商品名称: '+#stoStockVo.name+',批号: '+#stoStockVo.vbatchcode")
    public R updateState(@RequestBody RequestStoStockVo stoStockVo) {
        return wmsStoStockService.updateState(stoStockVo);
    }

    /**
     * 获取状态信息
     *
     * @param vo
     * @return
     */
    @ApiOperation("查询物料状态接口")
    @GetMapping("getStoStateInfo")
    public R getStoStateInfo(RequestStoStockVo vo) {
        return wmsStoStockService.getStoStateInfo(vo);
    }

    //=================批发版本库存相关接口================

    @ApiOperation("查询接口")
    @GetMapping("/queryAllStoStock")
    public AjaxResult queryAllStoStock(WmsStoStockDto dto) {
        return wmsStoStockService.queryAllStoStock(dto);
    }

    @ApiOperation("新增接口")
    @PostMapping("/add")
    public AjaxResult addStoStock(WmsStoStockDto dto) {
        return wmsStoStockService.add(dto);
    }

    @ApiOperation("修改接口")
    @PostMapping("/update")
    public AjaxResult updateStoStock(WmsStoStockDto dto) {
        return wmsStoStockService.update(dto);
    }
    @ApiOperation("修改状态接口")
    @PostMapping("/updateEnableState")
    public AjaxResult updateEnableState(String state,String id) {
        return wmsStoStockService.updateEnableState(state,id);
    }

    @ApiOperation("删除接口")
    @DeleteMapping("/deleteById")
    public AjaxResult deleteById(String id) {
        return wmsStoStockService.deleteById(id);
    }

    /**
     * 批量查询商品可用库存
     * @param productIds 商品ID列表（逗号分隔）
     * @return Map<商品ID, 可用库存数量>
     */
    @ApiOperation("批量查询商品可用库存")
    @GetMapping("/batchGetAvailableStock")
    public AjaxResult batchGetAvailableStock(@RequestParam String productIds) {
        try {
            String[] idArray = productIds.split(",");
            Map<String, BigDecimal> stockMap = new HashMap<>();

            for (String productId : idArray) {
                if (productId != null && !productId.trim().isEmpty()) {
                    BigDecimal stock = wmsStockService.getAvailableStockByProductId(productId.trim());
                    stockMap.put(productId.trim(), stock);
                }
            }

            return AjaxResult.success(stockMap);
        } catch (Exception e) {
            return AjaxResult.error("查询库存失败：" + e.getMessage());
        }
    }

}
